package palette;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import cuShapes.ShapeList;
import actionHandlers.CanvasListener;
import actionHandlers.InternalCloseListener;
import actionHandlers.ShapeDrawListener;
import java.awt.Dimension;
import java.util.Stack;

public class CanvasWindow extends JInternalFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1179450035315166177L;
	//private PropertiesSidebar sidebar;
	
	// This variable gives each shape that's created a unique id so that it
	// can be easily identified when reAddConnections inside cuShapes.Connectable
	// tries to find what shapes were connected before the save.
	private static int shapeId = 0;
	protected Canvas myCanvas;
	protected ShapeList shapeList;
	protected ShapeDrawListener shapeDrawListener;
	protected Stack<ShapeList> undoStack, redoStack;

	public CanvasWindow(String name){
		super(name,true,true,true,true);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.addInternalFrameListener(new InternalCloseListener());
		
		undoStack = new Stack<ShapeList>();
		redoStack = new Stack<ShapeList>();

		shapeList = new ShapeList();
		shapeDrawListener = new ShapeDrawListener();
		myCanvas = new Canvas();
		myCanvas.addMouseListener(shapeDrawListener);
		myCanvas.addMouseMotionListener(shapeDrawListener);
		myCanvas.setPreferredSize(new Dimension(550,350));
		myCanvas.addComponentListener(new CanvasListener());
		
		JScrollPane scroll = new JScrollPane(myCanvas);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		add(scroll);
		pack();
		setVisible(true);

		shapeList.addObserver(myCanvas);

	}
	
	
	/* Getters */
	public Canvas getCanvas(){ 				return myCanvas; 	}
	public ShapeList getShapeList() {		return shapeList;	}
	public Stack<ShapeList> getUndoStack(){	return undoStack;	}
	public Stack<ShapeList> getRedoStack(){	return redoStack;	}
	
	
	/*  Setters */
	public void setShapeList(ShapeList x){	shapeList = x;		}
	

	
	public int getShapeId() {
		return shapeId;
	}
	public void incShapeId() {
		shapeId++;
	}
	public void setShapeId(int id) {
		shapeId = id;
	}
}
